package com.mkl.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mkl.business.domain.Customer;
import com.mkl.business.mapper.CustomerMapper;
import com.mkl.business.service.CustomerService;
import com.mkl.business.vo.CustomerVo;
import com.mkl.system.common.Constant;
import com.mkl.system.common.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * @author king
 * @version 1.0
 * @date 2020/4/3 13:04
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public DataGridView loadAllCustomer(CustomerVo customerVo) {
        IPage<Customer> page = new Page<>(customerVo.getPage(), customerVo.getLimit());
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        qw.eq(customerVo.getAvailable() != null, "available", customerVo.getAvailable());
        qw.like(StringUtils.isNotBlank(customerVo.getCustomername()), "customername", customerVo.getCustomername());
        qw.like(StringUtils.isNotBlank(customerVo.getConnectionperson()), "connectionperson", customerVo.getConnectionperson());
        if (StringUtils.isNotBlank(customerVo.getPhone())) {
            qw.and(customerQueryWrapper -> customerQueryWrapper.like(StringUtils.isNotBlank(customerVo.getPhone()), "phone", customerVo.getPhone())
                    .or().like(StringUtils.isNotBlank(customerVo.getPhone()), "telephone", customerVo.getPhone()));
        }
        customerMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @CachePut(cacheNames = "com.mkl.business.service.impl.CustomerServiceImpl", key = "#result.id")
    @Override
    public Customer saveCustomer(Customer customer) {
        customerMapper.insert(customer);
        return customer;
    }

    @CachePut(cacheNames = "com.mkl.business.service.impl.CustomerServiceImpl", key = "#result.id")
    @Override
    public Customer updateCustomer(Customer customer) {
        Customer selectById = customerMapper.selectById(customer.getId());
        BeanUtil.copyProperties(customer, selectById, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        customerMapper.updateById(selectById);
        return selectById;
    }

    @Override
    public DataGridView getAllAvailableCustomer() {
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        qw.eq("available", Constant.AVAILABLE_TRUE);
        return new DataGridView(customerMapper.selectList(qw));
    }

    @Override
    public Integer getAllAvailableCustomerSum() {
        QueryWrapper<Customer> qw = new QueryWrapper<>();
//        qw.eq("available", Constant.AVAILABLE_TRUE);
        List<Customer> customers = customerMapper.selectList(qw);
        return customers.size();
    }

    @Cacheable(cacheNames = "com.mkl.business.service.impl.CustomerServiceImpl", key = "#id")
    @Override
    public Customer getById(Serializable id) {
        return super.getById(id);
    }

    @CacheEvict(cacheNames = "com.mkl.business.service.impl.CustomerServiceImpl", key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    public void deleteCustomerById(Integer id) {
        customerMapper.deleteById(id);
    }

    @Override
    public void batchDeleteCustomerByIds(Integer[] ids) {
        for (Integer id : ids) {
            customerMapper.deleteById(id);
        }
    }


}
